<?php
include './include/common.inc.php';
include './include/model.class.php';
$fields = model::get_all_fields();

$model = new model;
$action = isset($action) ? $action : 'manage';
$forward = isset($forward) ? $forward : (HTTP_REFERER ? HTTP_REFERER : $self_url);

if(!check_perms('rmodel'))
{
    showmessage('您没有权限进行此操作！');
}

switch($action)
{
    case 'add' :
        
        if(isset($submitform))
        {
            if(empty($data['name']))
            {
                $response->text_alert('error', 'name', '内容模型名称不能为空');
            }
            elseif(strlen($data['name'])>30)
            {
                $response->text_alert('error', 'name', '内容模型名称长度不能超过30字节');
            }
            elseif(!$model->check_unique($model->table, 'name', $data['name']))
            {
                $response->text_alert('error', 'name', '内容模型名称已经存在');
            }
            
            if(strlen($data['description'])>255)
            {
                $response->text_alert('error', 'description', '内容模型描述长度不能超过255字节');
            }

            if(empty($data['tablename']))
            {
                $response->text_alert('error', 'tablename', '数据表名不能为空');
            }
            elseif(strlen($data['tablename'])>30)
            {
                $response->text_alert('error', 'tablename', '数据表名长度不能超过30字节');
            }
            elseif(!$model->check_unique($model->table, 'tablename', $data['tablename']))
            {
                $response->text_alert('error', 'tablename', '数据表名已经存在');
            }
            elseif(!preg_match('/^[a-z]+$/i', $data['tablename']))
            {
                $response->text_alert('error', 'tablename', '数据表名只能由字母组成');
            }
            $response->real_execute();
            if($model->add($data))
            {
                $response->alert('添加成功');
				$response->redirect(format_url($self_url."?action=manage"));
            }
            else
            {
                $response->alert($model->error);
                $response->script('self.location=self.location');
            }
            $response->execute();
        }
        else
        {
            include template('admin', 'model_add');
        }
        break;

    case 'edit' :

        if(isset($submitform))
        {
            if(empty($data['name']))
            {
                $response->text_alert('error', 'name', '内容模型名称不能为空');
            }
            elseif(strlen($data['name'])>30)
            {
                $response->text_alert('error', 'name', '内容模型名称长度不能超过30字节');
            }
            if(strlen($data['description'])>255)
            {
                $response->text_alert('error', 'description', '内容模型描述长度不能超过255字节');
            }
            $response->real_execute();
            if($model->update($id, $data))
            {
                $response->alert('修改成功');
				$response->redirect(format_url($forward));
            }
            else
            {
                $response->alert('修改失败');
            }
            $response->execute();
        }
        else
        {
            $data = $model->getone($id);
            include template('admin', 'model_add');
        }
        break;

    case 'delete' :

        if($model->delete($id))
        {
            $response->alert('删除成功！');
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert($model->error);
        }
        $response->execute();
        break;

    case 'field_manage' :

        $data = $model->listfield($modelid);
        $modelinfo = $model->getone($modelid);
        include template('admin', 'field_manage');
        break;

    case 'field_add' :

        if(isset($submitform))
        {
            $formtype = $data['formtype'];
            include_once APP_ROOT."include/fields/$formtype.class.php";
            $fieldclass = "field_$formtype";
            $field = new $fieldclass;
            $errors = $model->field_check($data);
            $issystem = (isset($data['issystem']) && $data['issystem']==1) ? 1 : 0;
            $data['issystem'] = $issystem;
            if(!empty($errors))
            {
                foreach($errors as $e)
                {
                    $response->text_alert('error', $e['inputid'], $e['msg']);
                }
                $response->real_execute();
            }
            if($issystem==0)
            {
	            if(!$model->check_unique($model->table_field, 'field', $data['field'], "modelid='{$data['modelid']}'"))
	            {
	                $response->alert('该字段名已经存在！');
	                $response->real_execute();
	            }
            }
            if(!$field->check_settings($settings))
            {
                $response->text_alert('error', 'setting_errors', $field->error);
                $response->real_execute();
            }
            $data['settings'] = serialize($settings);

            if($model->field_add($data))
            {
                $response->alert('添加成功！');
                $response->redirect(format_url($self_url."?action=field_manage&modelid=$modelid"));
            }
            else
            {
                $response->alert($model->error);
            }
        }
        else
        {
            $modelinfo = $model->getone($modelid);
            include template('admin', 'field_add');
        }
        $response->execute();
        break;

    
    case 'field_edit' :

        if(isset($submitform))
        {
            $fielddata = $model->get_field($id);
            $formtype = $fielddata['formtype'];
            include_once APP_ROOT."include/fields/$formtype.class.php";
            $fieldclass = "field_$formtype";
            $field = new $fieldclass;
            $data['field'] = $fielddata['field'];
            $errors = $model->field_check($data);
            if(!empty($errors))
            {
                foreach($errors as $e)
                {
                    $response->text_alert('error', $e['inputid'], $e['msg']);
                }
                $response->real_execute();
            }
            if(!$field->check_settings($settings))
            {
                $response->text_alert('error', 'setting_errors', $field->error);
                $response->real_execute();
            }
            $data['settings'] = serialize($settings);
            $data['formtype'] = $formtype;
            $data['modelid'] = $fielddata['modelid'];
            $data['field'] = $fielddata['field'];
            $data['issystem'] = $fielddata['issystem'];
            
            if($model->field_update($id, $data))
            {
                $response->alert('修改成功！');
                $response->redirect(format_url($self_url."?action=field_manage&modelid=$modelid"));
            }
            else
            {
                $response->alert($model->error);
            }
        }
        else
        {
            $data = $model->get_field($id);
            $modelid = $data['modelid'];
            $modelinfo = $model->getone($modelid);
            include template('admin', 'field_add');
        }
        $response->execute();
        break;

    case 'field_delete' :

        if($model->field_delete($id))
        {
            $response->alert('删除成功！');
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert($model->error);
        }
        $response->execute();
        break;

    case 'field_status' :

        $action = $status == 0 ? '启用' : '禁止';
        if($model->field_status($id, $status))
        {
            $response->alert("$action 成功！");
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert($model->error);
        }
        $response->execute();
        break;

    case 'settings' :

        include_once APP_ROOT."include/fields/$formtype.class.php";
        $fieldclass = "field_$formtype";
        $field = new $fieldclass;
        $data = $model->get_field($fieldid);
        $data['settings'] = isset($data['settings']) ? $data['settings'] : '';
        echo $field->settings($data['settings']);
        break;

    case 'field_listorder' :

        $model->field_listorder($modelid, $info);
        $response->script('self.location=self.location');
        $response->execute();
        break;
    
    default :

        $data = $model->listinfo();
        include template('admin', 'model_manage');
}

?>
